﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint.LookupPicker (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Collections;
using Res = Sapiens.at.SharePoint.Fields.Resources.Sapiens_at_SharePoint_LookupPicker;
using System.Web.UI;

namespace Sapiens.at.SharePoint.Fields
{
    /// <summary>
    /// Represents the dialog of the <see cref="LookupPicker"/>.
    /// </summary>
    public class LookupPickerDialog : PickerDialog
    {
        SPWeb _web = null;
        SPList _list = null;

        /// <summary>
        /// Instantiates a new LookupPickerDialog using a <see cref="LookupPickerQueryControl"/>, a <see cref="TableResultControl"/> and a <see cref="LookupPicker"/>.
        /// </summary>
        public LookupPickerDialog()
            : base(new LookupPickerQueryControl(), new TableResultControl(), new LookupPicker())
        { }

        /// <summary>
        /// Gets the LookupPicker of this dialog.
        /// </summary>
        internal LookupPicker LookupPicker
        {
            get
            {
                return (LookupPicker)EditorControl;
            }
        }

        /// <summary>
        /// Gets the web this lookuppicker points to.
        /// </summary>
        public SPWeb Web
        {
            get
            {
                if (_web == null)
                {
                    SPSite site = SPContext.Current.Site;
                    _web = site.OpenWeb(LookupPicker.PickerData.WebID);
                }
                return _web;
            }
        }
        
        /// <summary>
        /// Disposes the current dialog and its <see cref="Web"/>.
        /// </summary>
        public override void Dispose()
        {
            if (_web != null)
            {
                _web.Dispose();
            }
            base.Dispose();
        }

        /// <summary>
        /// Gets the list this lookup picker points to.
        /// </summary>
        public SPList List
        {
            get
            {
                if (_list == null)
                {
                    _list = Web.Lists[LookupPicker.PickerData.ListID];
                }
                return _list;
            }
        }

        /// <summary>
        /// Initializes the <see cref="PickerDialog.DialogTitle"/> and <see cref="PickerDialog.Description"/>.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            DialogTitle = Res.LookupPickerDialogTitle;
            Description = String.Format(Res.LookupPickerDialogHeader, List.Title);
            base.OnLoad(e);
        }

        /// <summary>
        /// Creates the columns for the resulttable.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            TableResultControl resultControl = (TableResultControl)ResultControl;
            var columnDisplayNames = resultControl.ColumnDisplayNames;
            var columnNames = resultControl.ColumnNames;
            var columnWidths = resultControl.ColumnWidths;

            // Create a display column for each field defined in the field settings
            int percentage = (int)(100 / LookupPicker.PickerData.AllDisplayFields.Length);

            foreach (string fieldName in LookupPicker.PickerData.AllDisplayFields)
            {
                CreateResultColumn(columnDisplayNames, columnNames, columnWidths, percentage, fieldName);
            }
            RenderBranding();
            base.OnPreRender(e);
        }

        private void CreateResultColumn(ArrayList columnDisplayNames, ArrayList columnNames, ArrayList columnWidths, int percentage, string fieldName)
        {
            SPField field = List.Fields.GetField(fieldName);
            if (columnNames.Contains(fieldName)) { return; }
            columnDisplayNames.Add(field.Title);
            columnNames.Add(fieldName);
            columnWidths.Add(String.Format("{0}%", percentage));
        }

        private void RenderBranding()
        {
            var branding = new LiteralControl();
            branding.Text = Res.BrandingText;
            this.Controls.Add(branding);
        }
    }
}
